package com.cy.service;

import com.cy.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailServiceImpl implements UserDetailsService {


    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //Userinfo info=userDao.selectUserByUsername(username);
        //if(info==null)throw new UsernameNotFoundException();
        Users users = userService.QueryUser(username);
        System.out.println(users);
        if(users.getUsername()==null)//假设这是从数据库查询的信息
            throw new UsernameNotFoundException("user not exists");

        List<String> authority = userService.qeruyUserPermissions(users.getId());

        User user=new User(username,users.getPassword(),
                AuthorityUtils.createAuthorityList(authority.toArray(new String[]{})));

        System.out.println(user);
        return user;
    }
}
